#include <iostream>
#include <list>

using std::cout;
using std::endl;
using std::list;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

//函数对象的形式
struct CompareList
{
    bool operator()(const int &lhs, const int &rhs) const
    {
        cout << "struct CompareList" << endl;
        return lhs < rhs;
    }
};

void test()
{
    list<int> number = {1, 3, 5, 7, 3, 9, 6, 2, 3, 3, 3};
    display(number);

    cout << endl << "链表的去重unique函数" << endl;
    number.unique();//只能去除连续重复元素,所以可以先排序
    display(number);

    cout << endl << "链表的反转reverse函数" << endl;
    number.reverse();
    display(number);

    cout << endl << "链表的排序sort函数" <<endl;
    /* number.sort();//sort会从小到大排序 */
    /* std::less<int> tt; */
    /* number.sort(tt); */
    /* number.sort(std::less<int>()); */
    /* number.sort(std::greater<int>()); */
    number.sort(CompareList());
    display(number);

    cout << endl << "链表的去重unique函数" << endl;
    number.unique();
    display(number);

    cout << endl << "两个链表的合并merge函数" << endl;
    list<int> number2 = {30, 4, 8, 20, 10, 12};
    /* number2.sort(std::greater<int>()); */
    /* number.sort(std::greater<int>()); */
    /* number2.sort(std::greater<int>()); */
    number2.sort();
    display(number2);
    number.merge(number2);
    display(number);
    display(number2);

}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

